package com.authority.framework.base.service.impl;

import com.authority.framework.base.service.BaseService;
import com.authority.framework.base.util.StringUtils;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.db.KeyHolder;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @program: springboot-beatlsql
 * @description: 通用impl
 * @author: Mr.Yan
 * @create: 2018-10-11 09:38
 **/
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    //  调用 sqlManager接口
    @Autowired
    private SQLManager sqlManager;

    /**
     * 查询数据集
     * @param clazz
     * @param columnStr
     * @param tableName
     * @param whereStr
     * @param groupStr
     * @param orderStr
     * @return
     */
    @Override
    public List<T> findListTemplate(Class<T> clazz, String columnStr, String tableName, String whereStr,String groupStr,String orderStr) {
       String sqlTemplate = "SELECT"
                            + " "
                            + columnStr
                            + " "
                            + "FROM"
                            + " "
                            + tableName
                            + " "
                            + "WHERE 1=1";
       if (StringUtils.isNotEmpty(whereStr)){
           sqlTemplate += " "
                        + whereStr;
       }
       if(StringUtils.isNotEmpty(groupStr)){
           sqlTemplate += " "
                        + "GROUP BY"
                        + " "
                        + groupStr;
       }
       if(StringUtils.isNotEmpty(orderStr)){
           sqlTemplate += " "
                        + "ORDER BY"
                        + " "
                        + orderStr;
       }
        SQLReady sqlReady = new SQLReady(sqlTemplate);
        return sqlManager.execute(sqlReady,clazz);
    }

    /**
     * 查询一条数据
     * @param clazz
     * @param columnStr
     * @param tableName
     * @param whereStr
     * @param groupStr
     * @param orderStr
     * @return
     */
    @Override
    public T findOneTemplate(Class<T> clazz, String columnStr, String tableName, String whereStr, String groupStr, String orderStr) {
        List<T> list = this.findListTemplate(clazz, columnStr, tableName, whereStr, groupStr, orderStr);
        if(StringUtils.isNotNull(list)){
            if (list.size()>0) {
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * 根据主键查询实体
     *
     * @param clazz,pk
     * @return 返回实体
     */
    @Override
    public T findByPrimaryKey(Class<T> clazz, Integer pk) {
        return sqlManager.single(clazz, pk);
    }

    /**
     * 查询所有数据
     *
     * @param clazz
     * @return
     */
    @Override
    public List<T> findAll(Class<T> clazz) {
        return sqlManager.all(clazz);
    }

    /**
     * 查询总记录数
     *
     * @param clazz
     * @return
     */
    @Override
    public Long findAllCount(Class<T> clazz) {
        return sqlManager.allCount(clazz);
    }

    /**
     * 条件查询数据
     *
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public List<T> findListByWhere(Class<T> clazz, Object param) {
        return sqlManager.template(clazz, param, null);
    }

    /**
     * 条件查询数据总记录数
     *
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public Long findListByWhereCount(Class<T> clazz, Object param) {
        return sqlManager.templateCount(clazz, param);
    }

    /**
     * 条件查询一条数据
     *
     * @param clazz
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public T findOne(Class<T> clazz, Object param){
        List<T> list = sqlManager.template(clazz, param, null);
        if(StringUtils.isNotNull(list)){
            if (list.size()>0) {
                return list.get(0);
            }
        }
        return null;

    }

    /**
     * 分页查询数据
     *
     * @param clazz
     * @param param
     */
    @Override
    public List<T> pageInfo(Class<T> clazz, Object param, Long start, Long size) {
        return sqlManager.template(clazz, param, start, size, null);
    }

    /**
     * 分页查询返回 layui 数据模板
     *
     * @param sqlName
     * @param clazz
     * @param pageUtils
     * @return
     */
    @Override
    public LayDatas pageLayInfo(String sqlName, Class<T> clazz, PageUtils pageUtils) {
        PageQuery pageQuery = sqlManager.pageQuery(sqlName + ".pageInfo", clazz, pageUtils);
        return LayDatas.data(pageQuery);
    }

    /**
     * 插入一条数据
     *
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public Integer insert(Class<T> clazz, Object param) {
        KeyHolder holeder = new KeyHolder();
         sqlManager.insertTemplate(clazz, param, holeder);
         return Integer.valueOf(holeder.getKey().toString());

    }

    /**
     * 根据主键更新数据
     *
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public Integer update(Class<T> clazz, Object param) {
        return sqlManager.updateTemplateById(clazz, param);
    }

    /**
     * 根据主键删除数据
     *
     * @param clazz
     * @param pk
     * @return
     */
    @Override
    public Integer delete(Class<T> clazz, Object pk) {
        return sqlManager.deleteById(clazz, pk);
    }

    /**
     * 自己写的sql添加语句
     * @param sqlId
     * @param param
     * @return
     */
    @Override
    public Integer commonInsert(String sqlId,Object param){
        return sqlManager.insert(sqlId,param,null);
    }

    /**
     * 自己写的删除语句
     * @param sqlId
     * @param param
     * @return
     */
    @Override
    public Integer commonDelete(String sqlId, Object param) {
        return sqlManager.update(sqlId,param);
    }

    /**
     * 自己写的查询多套语句
     * @param sqlId
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public List<T> commonFindList(String sqlId,Class<T> clazz, Object param) {
        return sqlManager.select(sqlId,clazz,param);
    }

    /**
     **
     * 自己写sql查询一条语句
     * @param sqlId
     * @param clazz
     * @param param
     * @return
     */
    @Override
    public T commonFindOne(String sqlId, Class<T> clazz, Object param) {
        SQLReady sqlReady = new SQLReady("select * from user");
        sqlManager.execute(sqlReady,clazz);
        return sqlManager.selectUnique(sqlId,param,clazz);
    }
    /**
     **
     * 自己写sql修改语句
     * @param sqlId
     * @param param
     * @return
     */
    @Override
    public Integer commonUpdate(String sqlId ,Object param) {
        return sqlManager.update(sqlId,param);
    }
}
